<?php

namespace App\Http\Controllers\Admin\Donate;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Donates;
use App\Models\Orders;
use App\Models\Users;

/**
 * 捐赠管理控制器
 *
 * @author yh
 * @package App\Http\Controllers\Donates
 */
class DonatesController extends BaseAdminController
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 捐赠列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200318
     */
    public function index()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Donates::orderBy($sort, $order)->where('type', 1)->where('status', '<=', 2);
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            }
            if(request('number')){
                $query->where('number','LIKE', "%" . request('number') . "%");
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'number',
                'type',
                'exchange_nums',
                'exchange_username',
                'exchange_phone',
                'phone',
                'username',
                'status',
                'exchange_id',
                'type',
                'money',
                'title',
                'code',
                'score',
                'created_at'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.order.orders', [
                'orders' => $shops,
            ]);
        }

        /**
         * 兑换红包列表
         *
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public
        function order_money()
        {
            // 处理排序
            $sort = request('sort', 'id');
            $order = request('order', 'desc');

            $query = Orders::orderBy($sort, $order)->where('type', 2)->where('status', 1);
            if (request('title')) {
                $query->where('code', request('title'));
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'number',
                'type',
                'exchange_nums',
                'exchange_username',
                'exchange_phone',
                'phone',
                'username',
                'status',
                'exchange_id',
                'type',
                'money',
                'title',
                'code',
                'score',
                'created_at'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.order.order_money', [
                'orders' => $shops,
            ]);
        }

        /**
         * 已完成列表
         *
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public
        function order_finish()
        {
            // 处理排序
            $sort = request('sort', 'id');
            $order = request('order', 'desc');

            $query = Orders::orderBy($sort, $order)->where('status','>=', 3);

            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            }
            if(request('number')){
                $query->where('number','LIKE', "%" . request('number') . "%");
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'number',
                'type',
                'exchange_nums',
                'exchange_username',
                'exchange_phone',
                'phone',
                'username',
                'status',
                'exchange_id',
                'type',
                'money',
                'title',
                'code',
                'score',
                'created_at'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.order.order_finish', [
                'orders' => $shops,
            ]);
        }
        /**
         * 已完成列表
         *
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public
        function order_fail()
        {
            // 处理排序
            $sort = request('sort', 'id');
            $order = request('order', 'desc');

            $query = Orders::orderBy($sort, $order)->where('status','>=', 3);

            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            }
            if(request('number')){
                $query->where('number','LIKE', "%" . request('number') . "%");
            }
            // 取得列表
            $shops = $query->select([
                'id',
                'number',
                'type',
                'exchange_nums',
                'exchange_username',
                'exchange_phone',
                'phone',
                'username',
                'status',
                'exchange_id',
                'type',
                'money',
                'title',
                'code',
                'score',
                'created_at'
            ])->paginate(parent::PERPAGE);
            // 将查询参数拼接到分页链接中
            $shops->appends(app('request')->all());

            return view('admin.order.order_fail', [
                'orders' => $shops,
            ]);
        }
        /**
         * 兑换红包接口
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public
        function exchange_money()
        {
            $id = request('id');
            $code = request('code');
            $order = Orders::where('id', $id)->first();
            if (!$order) {
                return response_json(0, '', '订单不存在，请重试');
            }
            if ($order->status == 4) {
                return response_json(0, '', '红包已经兑换完成了，无法再次兑换');
            }
            if ($order->code != $code) {
                return response_json(0, '', '兑换码不正确');
            }
            $order->status = 4;
            $uid = $order->author_id;
            $user = Users::where('uid', $uid)->first();
            $sign = New Signs();
            $sign->uid = $order->author_id;
            $total = $user->score + ($order->exchange_num) * ($order->score);
            $user->score = $total;
            $user->save();
            $sign->type = 2;
            $sign->add_type = 1;
            $sign->scores = $total;
            $sign->username = $user->username;
            $sign->save();
            $order->save();
            return response_json(1, '', 'success');
        }
        /**
         * 确认完成兑换商品订单
         * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
         * @author yh 20200318
         */
        public  function finish($id){
            $order = Orders::where('id', $id)->first();
            $user = Users::where('uid', $order['author_id'])->first();
            $total = $user->score + ($order->exchange_num) * ($order->score);
            $user->score = $user->score + ($order->exchange_num) * ($order->score);
            $user->save();
            $sign = New Signs();
            $sign->uid = $order->author_id;
            $sign->type = $order->type;
            $sign->add_type = 1;
            $sign->scores = $total;
            $sign->username = $user->username;
            $sign->save();
            $order->status = 3;
            $order->save();
            return $this->success();
        }
    /**
     * 删除用户
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author lxp 20160713
     */
    public function delete($id)
    {
        if (request()->ajax() && intval($id)) {
            // 判断用户是否可以被删除
            // 删除用户
            Orders::destroy($id);
            return $this->success();
        }
    }
    }